package com.neuedu.yyzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.entity.Menu;
import com.neuedu.yyzx.entity.RoleMenu;
import com.neuedu.yyzx.entity.UserRole;
import com.neuedu.yyzx.mapper.MenuMapper;
import com.neuedu.yyzx.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.yyzx.service.IRoleMenuService;
import com.neuedu.yyzx.service.IUserRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author 第7组....
 * @since 2021-05-22
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Resource
    IUserRoleService umsUserRoleService;

    @Resource
    IRoleMenuService umsRolePermissionService;




    @Override
    public Object list(Menu menu1 , Page page1) {

        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        IPage<Menu> page = this.page(page1,wrapper);
        List<Menu> list = page.getRecords();
        for(Menu permission : list) {
            getChildren(permission);
        }
        return page;
    }

    @Override
    public boolean add(Menu umsPermission) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("name",umsPermission.getName());
        Menu query = this.getOne(wrapper);
        if(query == null )
            return this.save(umsPermission);
        return false;
    }




    /**
     * 参数为父权限对象
     * 通过父权限获取子权限
     *
     * */
    private void getChildren(Menu umsPermission) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",umsPermission.getId());
        List<Menu> list = this.list(wrapper);
        for(Menu permission : list) {
//            if(permission.getType() != 0) {
            getChildren(permission);
//            }
        }
        umsPermission.setChildren(list);
    }


    @Override
    public List<Menu> userPermissionList(Integer userId) {
        //1根据用户id查询出所有的角色ums_user_role
        QueryWrapper<UserRole> umsUserQueryWrapper = new QueryWrapper<>();
        umsUserQueryWrapper.eq("user_id",userId);
        UserRole umsUserRole = umsUserRoleService.getOne(umsUserQueryWrapper);
        if(umsUserRole==null || umsUserRole.getRoleId() == null){
            return null;
        }

        //System.out.println(umsUserRole.getRoleId()); //
        String[] roleIds = umsUserRole.getRoleId().split(",");
        //根据角色获取权限id集合
        QueryWrapper<RoleMenu> umsRolePermissionWrapper = new QueryWrapper<>();
        umsRolePermissionWrapper.in("role_id",roleIds);
        List<RoleMenu> rolePermissionList = umsRolePermissionService.list(umsRolePermissionWrapper);
        if(rolePermissionList ==null ){
            return null;
        }

        Set permissionIdSet = new HashSet();
        for (RoleMenu umsRolePermission : rolePermissionList) {
            permissionIdSet.addAll(Arrays.asList(umsRolePermission.getMenuId().split(",")));
        }

//        permissionIdSet.forEach(System.out::println);

        //根据权限id查询权限列表
        QueryWrapper<Menu> umsPermissionQueryWrapper = new QueryWrapper<>();
        umsPermissionQueryWrapper.in("id",permissionIdSet);
        List<Menu> allPermission = this.list(umsPermissionQueryWrapper);
        //目标，将顶层菜单放入
        List<Menu> frontPermissionList = new ArrayList();
        for (Menu permission : allPermission) {
            if(permission.getParentId() == 0){
                setChildrenPermission(permission,allPermission);
                frontPermissionList.add(permission);
            }
        }
        return frontPermissionList;
    }

    private void setChildrenPermission(Menu permission, List<Menu> allPermission) {
        List<Menu> children = new ArrayList();
        for (Menu umsPermission : allPermission) {
            if(permission.getId() == umsPermission.getParentId()){
                setChildrenPermission(umsPermission,allPermission);
                children.add(umsPermission);
            }
        }
        permission.setChildren(children);
    }



    @Override
    public List<Menu> userPermissionListValidate(Integer userId) {
        //1根据用户id查询出所有的角色ums_user_role
        QueryWrapper<UserRole> umsUserQueryWrapper = new QueryWrapper<>();
        umsUserQueryWrapper.eq("user_id",userId);
        UserRole umsUserRole = umsUserRoleService.getOne(umsUserQueryWrapper);
        if(umsUserRole==null || umsUserRole.getRoleId() == null){
            return null;
        }

        //System.out.println(umsUserRole.getRoleId()); //
        String[] roleIds = umsUserRole.getRoleId().split(",");
        //根据角色获取权限id集合
        QueryWrapper<RoleMenu> umsRolePermissionWrapper = new QueryWrapper<>();
        umsRolePermissionWrapper.in("role_id",roleIds);
        List<RoleMenu> rolePermissionList = umsRolePermissionService.list(umsRolePermissionWrapper);
        if(rolePermissionList ==null ){
            return null;
        }

        Set permissionIdSet = new HashSet();
        for (RoleMenu umsRolePermission : rolePermissionList) {
            permissionIdSet.addAll(Arrays.asList(umsRolePermission.getMenuId().split(",")));
        }

        //根据权限id查询权限列表
        QueryWrapper<Menu> umsPermissionQueryWrapper = new QueryWrapper<>();
        umsPermissionQueryWrapper.in("id",permissionIdSet);
        List<Menu> allPermission = this.list(umsPermissionQueryWrapper);
        return allPermission;
    }
}
